<template>
    <div style="margin: 20px;">
        
        <el-dialog title="发票打印" :visible.sync="dialogFormVisible" style="float:left">
      
      <el-form :model="backInvoiceForm" style="margin-top:20px">
        <el-form-item label="发票号" :label-width="formLabelWidth">
          <el-input style="width:80%" v-model="backInvoiceForm.invoiceId" autocomplete="off" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="项目名" :label-width="formLabelWidth">
          <el-input style="width:80%" v-model="backInvoiceForm.proName" autocomplete="off" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="单价" :label-width="formLabelWidth">
          <el-input style="width:80%" v-model="backInvoiceForm.sinPrice" autocomplete="off" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="数量" :label-width="formLabelWidth">
          <el-input style="width:80%" v-model="backInvoiceForm.num" autocomplete="off" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="金额" :label-width="formLabelWidth">
          <el-input style="width:80%" v-model="backInvoiceForm.expense" autocomplete="off" :disabled="true"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="backRegisterInvoice()">打印</el-button>
      </div>
      
    </el-dialog>

        <el-container>           
        <el-header >        
          <span style="font-size:60px">退号</span>
          <el-link type="primary" href="/#/initMain" style="float:right"><b>返回首页</b></el-link>
        </el-header>
        <el-main style="text-align:left">               
            <span ><b>患者信息查询</b></span><br/>
            <div>
            <el-form  :model="returnANumForm" :rules="rules" ref="returnANumForm" label-width="60px" class="demo-returnANumForm">  
                <el-row :gutter="20">
                    <el-col :span="8">            
                <el-form-item label="病历号" prop="patientId" >
                <el-input v-model="returnANumForm.patientId"  placeholder="病历号"></el-input>
                </el-form-item>
                    </el-col>
                    
                <el-button type="primary" @click="findPatient" icon="el-icon-search">搜索</el-button>
                </el-row>
            </el-form>
            </div>
                
                <span ><b>患者信息确认</b></span>
                <table> 
                    <tr>
                        <td>姓名</td>        
                
                        <td><el-input v-model="name"  placeholder="姓名"></el-input></td><td></td>
                        <td>身份证号</td>
                        <td><el-input v-model="personId"  placeholder="身份证号"></el-input></td><td></td>
                        <td>家庭住址</td>
                        <td><el-input v-model="address"  placeholder="家庭住址"></el-input></td>
                    </tr>
                    
                </table>
                
                

              
                
                
                <hr/>
                <span ><b>患者挂号信息</b></span>
                <!--<el-table
                    :data="tableDataM" 
                    style="width: 100%"
                    max-height="250">
                    <el-table-column
                      label="病历号"
                        width="150">
                      <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.patientId }}</span>
                      </template>
                    </el-table-column>
                    <el-table-column
                       label="姓名"
                        width="120">
                        <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.name }}</span>
                      </template>
                    </el-table-column>
                    <el-table-column
                      label="身份证号"
                        width="200">
                        <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.identified }}</span>
                      </template>
                    </el-table-column>
                    <el-table-column
                    label="挂号日期"
                    width="180">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.registerTime }}</span>
                      </template>
                    </el-table-column>
                    <el-table-column
                    
                    label="挂号级别"
                    width="120">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.registerLevelName }}</span>
                      </template>
                    </el-table-column>
                    <el-table-column
                    prop="room"
                    label="看诊科室"
                    width="200">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.deptName }}</span>
                      </template>
                    </el-table-column>
                    <el-table-column
                    prop="state"
                    label="看诊状态"
                    width="120">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.stateName }}</span>
                      </template>
                    </el-table-column>
                    <el-table-column
                    
                    label="操作"
                    width="120">
                    <el-button
                        @click="deleteRow()"
                        type="danger"
                        size="mini">
                        退号
                        </el-button>
                    </el-table-column>
                </el-table>-->
                 <el-table
                    :data="tableDataM"
                    style="width: 100%"
                    >
                    <el-table-column
                        prop="patientId"
                        label="病历号"
                        width="150">
                    </el-table-column>
                    <el-table-column
                        prop="name"
                        label="姓名"
                        width="120">
                    </el-table-column>
                    <el-table-column
                        prop="identified"
                        label="身份证号"
                        width="200">
                    </el-table-column>
                    <el-table-column
                    prop="registerTime"
                    label="挂号日期"
                    width="200">
                    </el-table-column>
                    <el-table-column
                    prop="registerLevelName"
                    label="挂号级别"
                    width="120">
                    </el-table-column>
                    <el-table-column
                    prop="deptName"
                    label="看诊科室"
                    width="200">
                    </el-table-column>
                    <el-table-column
                    prop="stateName"
                    label="看诊状态"
                    width="120">
                    </el-table-column>
                   <el-table-column
                    fixed="right"
                    label="操作"
                    width="100">
                    
                    <template slot-scope="scope">
                        <el-button
                        @click.native.prevent="deleteRow(scope.$index)"
                        type="danger"
                        size="small">
                        退号
                        </el-button>
                    </template>
                    </el-table-column>
                </el-table>

            </el-main>
        </el-container>
    </div>
</template>

<style>
  .el-header {
    background-color: #B3C0D1;
    color: #333;
    line-height: 60px;
    
  }
</style>

<script>
import axios from "axios";
  export default {
    data() {
      return {
        name:'',
        personId:'',
        address:'',
        cost:0,
        dialogFormVisible:false,
        formLabelWidth:'120px',
        location:0,

        returnANumForm:{
            patientId:''           
        },
        backInvoiceForm:{
          invoiceId:'',
          proName:'退号',
          sinPrice:0,
          num:1,
          expense:0,
        },
        rules: {
          patientId: [
            { required: true, message: '请输入病历号', trigger: 'blur' },
            { min: 1, max: 20, message: '长度在 1 到 9 个字符', trigger: 'blur' }
          ],
        },
        tableDataM: [],/*[{
          patientId: '000000001',
          name: '王小虎',
          personId: '371481199701012230',
          date1: '2016-10-2',
          registerLevel: '专家号',
          room: '呼吸内科',
          state:'未*/
      };
    },

    methods: {
      /*submitForm(formName) {
        this.$refs[formName].validate((valid) => {
            if (valid) {
                alert('submit!');
            } else {
                console.log('error submit!!');
                return false;
            }
        }); 
    },*/
      findPatient:function(){
        var self=this;
        axios
        .get("/api/reception/findPatient", {
          params: {
            patientId: self.returnANumForm.patientId
          }
        })
        .then( res=> {
          
          console.log(res.data);
          self.tableDataM=[];
          //self.tableDataM[0]=res.data;
          if(res.data.stateId==0){//未看诊的才能退号
            self.tableDataM.push(res.data);
          }
          
          self.name = res.data.name;
          console.log(name);
          self.personId = res.data.identified;
          self.address =res.data.address;
          self.cost=res.data.cost;
          
          console.log(self.tableDataM);
          //sessionStorage.setItem("patient", JSON.stringify(res.data));
              
          })
          .catch(function(error) {
            console.log(error);
          });
        
      },
      
      deleteRow(index) {
        this.dialogFormVisible=true;
        
        this.backInvoiceForm.invoiceId=new Date().getTime();
          this.backInvoiceForm.proName='退号';
          this.backInvoiceForm.sinPrice=-this.cost;
          this.backInvoiceForm.num=1;
          this.backInvoiceForm.expense=-this.cost;
          //rows.splice(index, 1);
         // tableData[0].state="已退号";
         this.location=index;
        
        },
      backRegisterInvoice(){
        axios
        .get("/api/reception/pushPatient", {
          params: {
            patientId: this.returnANumForm.patientId
          }
        })
        .then(res => {
          console.log(res.data);
           var invoice=JSON.stringify({
                invoiceId:this.backInvoiceForm.invoiceId,
                invoiceDatetime:new Date(),
                //payTypeId:,
                isLegal:1,
                printTimes:1,
                money:this.backInvoiceForm.expense,
                });
              console.log(invoice);
              axios
              .post("/api/reception/registerInvoice",invoice,{
                headers:{"Content-Type":"application/json;charset=utf-8"}
                /*// params :{
                //   ppk:patiProKey,
                //   pp:patPro
                // }*/
              }).then(res => {
              console.log(res.data);
              
              
              var patiPro=JSON.stringify({
                  patientId:this.returnANumForm.patientId,
                  projectName:this.backInvoiceForm.proName,
                  invoiceId:this.backInvoiceForm.invoiceId,
                  projectNum:this.backInvoiceForm.num,
                  projectDatetime:new Date(),//new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) ,
                  stateId:2,//已退号状态
                });
                console.log(patiPro);
                axios
                .post("/api/reception/insertPatiPro",patiPro,{
                  headers:{"Content-Type":"application/json;charset=utf-8"}
                  /*// params :{
                  //   ppk:patiProKey,
                  //   pp:patPro
                  // }*/
                })
                .then(res => {
                  console.log(res.data);
                  this.tableDataM.splice(this.location,1);
                  alert('打印成功');
                  this.dialogFormVisible=false;
                  })
                .catch(function(error) {
                  console.log(error);
                });
                axios
                .get("/api/reception/deletePatiPro", {
                  params: {
                    patientId: this.returnANumForm.patientId,
                    projectName:'挂号'
                  }
                })
                .then(res => {
                  console.log(res.data);
                })
                .catch(function(error) {
                    console.log(error);
                  });


              }).catch(function(error) {
              console.log(error);
              
            });
          })
          .catch(function(error) {
            console.log(error);
          });
          
      }

    },
    
  }
</script>